home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 01 / kermit / wntmsc.c < prev   
C/C++ Source or Header  |  1990-12-31  |  7KB  |  240 lines

  1. /* 
  2.  * Less frequently used functions for WNTERM
  3.  *
  4.  * Written by
  5.  * William S. Hall
  6.  * 3665 Benton Street, #66
  7.  * Santa Clara, CA 95051
  8. */
  9.  
  10. #define NOKANJI
  11. #define NOATOM
  12. #define NOSOUND
  13. #include <windows.h>
  14. #include <string.h>
  15. #include "ttycls.h"
  16. #include "wnterm.h"
  17. #include "wntdlg.h"
  18.  
  19. /* local function declarations */
  20. static void NEAR SetCommInit(HWND hDlg);
  21. static BOOL NEAR SetCommValues(HWND hDlg);
  22.  
  23. /* procedure for the about box */
  24. BOOL FAR PASCAL AboutBoxProc(hDlg, message, wParam, lParam)
  25. HWND hDlg;
  26. unsigned message;
  27. WORD wParam;
  28. LONG lParam;
  29. {
  30.  
  31.     switch (message) {
  32.     case WM_INITDIALOG:    /* nothing to initialize */
  33.         break;
  34.     case WM_COMMAND:    /* system command */
  35.         switch (wParam) {
  36.         case IDOK:    /* respond only to these parameter */
  37.         case IDCANCEL:
  38.             EndDialog(hDlg,TRUE);
  39.             break;
  40.         default:
  41.             return FALSE;    /* windows will handle this */
  42.         }
  43.         break;
  44.  
  45.     default:
  46.         return FALSE;    /* and, windows will process this as well */
  47.     }
  48.     return TRUE;        /* if we process message, return TRUE */
  49. }
  50.  
  51. /* display a simple message box according to the string id */
  52. void ShowMessage(HWND hWnd, int msgnum)
  53. {
  54.  
  55.     char szMessage[80];
  56.  
  57.   /* get the string corresponding to msgnum */
  58.     LoadString(hInst, msgnum, (LPSTR)szMessage, sizeof(szMessage));
  59.     
  60.   /* put up this simple message box */
  61.     MessageBox(hWnd,(LPSTR)szMessage, (LPSTR)szAppName, 
  62.         MB_ICONASTERISK | MB_OK);
  63.  
  64. }
  65.  
  66. /* set the communications parameters selected by user */
  67. BOOL FAR PASCAL SetCommParams(hDlg, message, wParam, lParam)
  68. HWND hDlg;
  69. unsigned message;
  70. WORD wParam;
  71. LONG lParam;
  72. {
  73.  
  74.     int result;
  75.  
  76.     switch (message) {
  77.     case WM_INITDIALOG:        /* initialize the box parameters */
  78.         SetCommInit(hDlg);
  79.         break;
  80.  
  81.     case WM_COMMAND:    
  82.         switch (wParam) {
  83.         case IDD_BAUDRATE: /* if nothing in baudrate box, disable OK */
  84.             if (HIWORD(lParam) == EN_CHANGE) {
  85.                 result = (int)SendDlgItemMessage(hDlg,wParam,
  86.                         WM_GETTEXTLENGTH,0,0L);
  87.             EnableWindow(GetDlgItem(hDlg, IDOK), result);
  88.             }
  89.             break;
  90.  
  91.         case IDOK:    /* set comm parameters from values in box */
  92.             result = SetCommValues(hDlg);
  93.             if (result)
  94.                 EndDialog(hDlg, TRUE);
  95.             break;
  96.         
  97.         case IDCANCEL:        /* user wants no changes */
  98.             EndDialog(hDlg, FALSE);
  99.             break;
  100.  
  101.         case IDD_COM1:    /* highlight the button selected */
  102.         case IDD_COM2:
  103.         case IDD_COM3:
  104.         case IDD_COM4:
  105.             CheckRadioButton(hDlg, IDD_COM1, IDD_COM4, wParam);
  106.             break;
  107.  
  108.         case IDD_SEVENBITS:  /* highlight the button selected */
  109.         case IDD_EIGHTBITS:
  110.             CheckRadioButton(hDlg,IDD_SEVENBITS,IDD_EIGHTBITS,wParam);
  111.             break;
  112.  
  113.         case IDD_NOPARITY:   /* highlight the button selected */
  114.         case IDD_ODDPARITY:
  115.         case IDD_EVENPARITY:
  116.         case IDD_MARKPARITY:
  117.         case IDD_SPACEPARITY:
  118.             CheckRadioButton(hDlg,IDD_NOPARITY,IDD_SPACEPARITY,wParam);
  119.             break;
  120.  
  121.         default:
  122.             return FALSE;
  123.         }
  124.         break;
  125.  
  126.     default:
  127.         return FALSE;
  128.     }
  129.     return TRUE;
  130. }
  131.  
  132. /* read the communications DCB and initialize the dialog box accordingly */
  133. static void NEAR SetCommInit(HWND hDlg)
  134. {
  135.  
  136.   /* enter the baud rate into the baudrate edit control */
  137.     SetDlgItemInt(hDlg,IDD_BAUDRATE,CommData.BaudRate,FALSE);
  138.  
  139.   /* highlight the corresponding radio buttons */
  140.     CheckRadioButton(hDlg,IDD_SEVENBITS, IDD_EIGHTBITS,
  141.                 IDD_SEVENBITS + CommData.ByteSize -7);
  142.     CheckRadioButton(hDlg,IDD_NOPARITY, IDD_SPACEPARITY,
  143.                 IDD_NOPARITY + CommData.Parity - NOPARITY);
  144.     CheckRadioButton(hDlg,IDD_COM1, IDD_COM4, IDD_COM1 + CommData.Id);
  145.  
  146. }
  147.  
  148. /* set communications values; if failure, try to restore the old ones */
  149. static BOOL NEAR SetCommValues(HWND hDlg)
  150. {
  151.  
  152.     register int i, state;
  153.     short newcid;
  154.     char comstr[10];
  155.     char wintitle[50];
  156.     BYTE newid;
  157.     BYTE oldid = CommData.Id;            /* save the current values */
  158.     WORD oldbaud = CommData.BaudRate;
  159.     BYTE oldsize = CommData.ByteSize;
  160.     BYTE oldpar = CommData.Parity;
  161.     BYTE oldstop = CommData.StopBits;
  162.  
  163.   /* read the rate from the edit control */
  164.     CommData.BaudRate = GetDlgItemInt(hDlg,IDD_BAUDRATE,NULL, FALSE);
  165.  
  166.     for (i = IDD_COM1; i <= IDD_COM4; i++) {
  167.       /* get the button which is checked */
  168.     state = (int)SendDlgItemMessage(hDlg, i, BM_GETCHECK, 0, 0L);
  169.     if (state) {
  170.         newid = (BYTE)(i - IDD_COM1);    /* convert to comm cid */
  171.         break;
  172.     }
  173.     }
  174.   /* read the button which is checked */
  175.     for (i = IDD_SEVENBITS; i <= IDD_EIGHTBITS; i++) {
  176.     state = (int)SendDlgItemMessage(hDlg, i, BM_GETCHECK, 0, 0L);
  177.     if (state) {
  178.       /* convert to a communications byte size parameter */
  179.         CommData.ByteSize = (BYTE)(i - IDD_SEVENBITS + 7);
  180.         break;
  181.     }
  182.     }
  183.   /* read the button which is checked */
  184.     for (i = IDD_NOPARITY; i <= IDD_SPACEPARITY; i++) {
  185.     state = (int)SendDlgItemMessage(hDlg, i, BM_GETCHECK, 0, 0L);
  186.     if (state) {
  187.       /* convert to a communications parity value */
  188.         CommData.Parity = (BYTE)(i - IDD_NOPARITY + NOPARITY);
  189.         break;
  190.     }
  191.     }
  192.     if (CommData.BaudRate <= 200)    /* two stop bits in this case */
  193.     CommData.StopBits = TWOSTOPBITS;
  194.     else 
  195.     CommData.StopBits = ONESTOPBIT;
  196.  
  197.   /* if requesting a new comm port, try to open it first */
  198.     if (newid != oldid) {
  199.         LoadString(hInst, IDS_COM1 + newid, (LPSTR)comstr, sizeof(comstr));
  200.     newcid = OpenComm((LPSTR)comstr, INQUESIZE, OUTQUESIZE);
  201.       /* if successful, close the old one */
  202.     if (newcid >= 0) {
  203.         state = CloseComm(cid);
  204.       /* old one closed, so update window title and cid number */
  205.         if (state == 0) {
  206.         cid = newcid;
  207.             CommData.Id = (BYTE)cid;
  208.             LoadString(hInst,IDS_WINTITLE,(LPSTR)wintitle,sizeof(wintitle));
  209.         strcat(wintitle,comstr);
  210.         SetWindowText(MWnd.hWnd, (LPSTR)wintitle);                
  211.         }
  212.         else {
  213.           /* failed, go back to old port and show error */
  214.         CloseComm(newcid);
  215.         ShowMessage(hDlg, IDS_CANNOTOPENPORT);
  216.         return FALSE;
  217.         }
  218.     }
  219.     else {
  220.      /* failed to open new port, show error */
  221.         ShowMessage(hDlg, IDS_CANNOTOPENPORT);
  222.         return FALSE;
  223.     }
  224.     }
  225.   /* set the state of the comm DCB */
  226.     state = SetCommState((DCB FAR *)&CommData);
  227.   /* if unsuccessful, restore all the old values and show error */
  228.     if (state) {
  229.     CommData.BaudRate = oldbaud;
  230.     CommData.ByteSize = oldsize;
  231.     CommData.Parity = oldpar;
  232.     CommData.StopBits = oldstop;
  233.     SetCommState((DCB FAR *)&CommData);
  234.     SetCommInit(hDlg);
  235.         ShowMessage(hDlg, IDS_COMMSETERROR);
  236.     return FALSE;
  237.     }
  238.     return TRUE;
  239. }
  240.